#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <filesystem>
#include "log.hpp"
#include "util.hpp"

using std::filesystem::directory_iterator;

const std::string PREFFIX = "/post-images/";
const std::string PICLINK = "<img src=\"";
const std::string SRC_DIR = "/Users/yyo/Documents/Gridea/posts/";

class Essay
{
private:
    std::string _filename;
    std::string _content;

    std::vector<int> index_v;
    std::vector<std::string> _content_block;

public:
    Essay(std::string file): _filename(file)
    {}

private:
    void FindPicLink(const std::string& pic_link, const std::string& preffix)
    {
        size_t pos = 0;
        while (true)
        {
            pos = _content.find(pic_link, pos);

            if (pos == std::string::npos)
            {
                break;
            }
            else if (_content.substr(pos + pic_link.size(), 4) == "http" || // 防止在线图片
                    _content.substr(pos + pic_link.size(), preffix.size()) == preffix) // 防止重复添加
            {
                pos += pic_link.size();
            }
            else
            {
                pos += pic_link.size(); // 将下标向后移动到 <img src=" 的下一个位置，方便进行后续操作
                index_v.push_back(pos);
            }
        }

        // LOG(BEBUG) << "find link pos: ";
        // for (auto e : index_v) std::cout << e << " "; std::cout << std::endl;
    }

    void SplitContent()
    {
        int begin = 0, end = 0;

        for (auto& pos : index_v)
        {
            begin = end;
            end = pos;

            std::string block(_content, begin, end - begin); // 将文章内容切分成一块一块
            _content_block.push_back(block);
        }

        _content_block.push_back(_content.substr(end));

        // LOG(BEBUG) << "split block: ";
        // for (auto e : _content_block) std::cout << e << " "; std::cout << std::endl;
    }

    void AddPreffix(const std::string& preffix)
    {
        // 清空文章内容并提前开好空间
        _content.clear();
        _content.reserve(_content.size() + index_v.size() * preffix.size());

        // 将大块内容尾插preffix后重新插入到文章中
        for (int i = 0; i < _content_block.size() - 1; i++)
            _content += _content_block[i] + preffix;

        _content += _content_block.back();

        // LOG(DEBUG) << "_content: " << _content << std::endl;
    }

public:
    void HandlerTask(const std::string& pic_link = PICLINK, const std::string& preffix = PREFFIX)
    {
        if (!Util::ReadFile(_filename, &_content, true))
            LOG(ERROR) << _filename << "open failed" << std::endl;

        FindPicLink(pic_link, preffix);
        SplitContent();
        AddPreffix(preffix);

        if (!Util::WriteFile(_filename, _content))
            LOG(ERROR) << _filename << "write failed" << std::endl;

        LOG(INFO) << "handler file succ: " << _filename << std::endl;
    }
};

// directory_iterator
class DirIter
{
private:
    std::string src_dir = SRC_DIR;
public:
    void operator()(const std::string& src_dir = SRC_DIR)
    {
        for (auto& file : directory_iterator(src_dir))
        {
            if (file.path().has_extension()) // 不是目录是文件
            {
                Essay essay(file.path().string());
                essay.HandlerTask();
            }
        }

    }
};